package cn.wolfcode.service.impl;

import cn.wolfcode.mapper.CustomerReportMapper;
import cn.wolfcode.qo.CustomerReportQuery;
import cn.wolfcode.service.CustomerReportService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 杨兴健
 * @Date 2020/7/24 18:26
 */
@Service
public class CustomerReportServiceImpl implements CustomerReportService {

    @Autowired
    private CustomerReportMapper customerReportMapper;

    @Override
    public PageInfo<HashMap> selectForList(CustomerReportQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<HashMap> lists = customerReportMapper.selectForList(qo);

        return new PageInfo<>(lists);
    }

    @Override
    public Map<String, String> selectAllBar(CustomerReportQuery qo) {
        HashMap<String, String> map = new HashMap<>();
        List l1 = new ArrayList();
        List l2 = new ArrayList();
        List<HashMap> lists = customerReportMapper.selectAll(qo);
        for (HashMap m : lists) {
            l1.add(m.get("groupType"));
            l2.add(m.get("number"));
        }
        map.put("groupType", JSON.toJSONString(l1));
        map.put("number", JSON.toJSONString(l2));
        return map;
    }

    @Override
    public Map<String, String> selectAllPie(CustomerReportQuery qo) {
        HashMap<String, String> map = new HashMap<>();

        List<HashMap> list = customerReportMapper.selectAll(qo);
        List l1 = new ArrayList();
        List l2 = new ArrayList();

        for (HashMap hashMap : list) {

            l1.add(hashMap.get("groupType"));
            HashMap map1 = new HashMap<>();
            map1.put("name", hashMap.get("groupType"));
            map1.put("value", hashMap.get("number"));
            l2.add(map1);
        }
        map.put("groupType", JSON.toJSONString(l1));
        map.put("number", JSON.toJSONString(l2));
        return map;
    }
}
